﻿// <copyright file="Gravity.cs" company="Community Combat Simulator">
//    Community Combat Simulator: a free combat flight simulator.
//    Copyright (C) 2008 Aaron Schinkowitch
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see http://www.gnu.org/licenses/.
// </copyright>
namespace CCSim.Physics
{
    using System;
    using CCSim.Util;
    using CCSim.Vecmath;

    /// <summary>
    /// Calculates the force of gravity on an object, based on the position of its center of mass.
    /// </summary>
    internal class Gravity
    {
        /// <summary>
        /// The mass of the earth in kg multiplied by the universal gravitational constant.
        /// </summary>
        private const double EarthMassMultipliedByG = 5.9736E24 * 6.67428E-11;
        
        /// <summary>
        /// Initializes a new instance of the Gravity class.
        /// </summary>
        internal Gravity()
        {
        }
        
        /// <summary>
        /// Calculates the force of gravity acting on a rigid body.
        /// </summary>
        /// <param name="rigidBody">The rigid body.</param>
        /// <returns>The force of gravity acting upon the rigid body.</returns>
        internal virtual Vector CaluculateForce(RigidBody rigidBody)
        {
            if (rigidBody == null)
            {
                throw new ArgumentNullException("rigidBody");
            }
            
            Vector position = rigidBody.Position;
            
            // G * M1 * M2 / r^2
            double magnitude = -(EarthMassMultipliedByG * rigidBody.Mass) / position.LengthSquared;
            
            return position.Normalize().Scale(magnitude);
        }
    }
}
